// 添加菜单
const { Menu, dialog, BrowserWindow } = require("electron").remote;
const iconv = require("iconv-lite");
const fs = require("fs");
var textarea = document.querySelector("textarea");
var terMenu = [
  {
    label: "菜单",
    submenu: [
      {
        label: "打开",
        click: () => {
          dialog
            .showOpenDialog({
              title: "打开文件",
              filters: [
                {
                  name: "txt",
                  extensions: ["txt", "html", "js", "java", "c"],
                },
              ],
            })
            .then((result) => {
              // 读取文件
              // console.log(result.filePaths[0]);
              // 获取数据
              var str = fs.readFileSync(result.filePaths[0]).toString();
              textarea.value = str;
            })
            .catch((err) => {
              console.log(err);
            });
        },
      },
      {
        label: "用其他编码方式打开",
        submenu: [
          {
            label: "用GBK编码打开",
            click: () => {
              dialog
                .showOpenDialog({
                  title: "打开文件",
                  filters: [
                    {
                      name: "txt",
                      extensions: ["txt", "html", "js", "java", "c"],
                    },
                  ],
                })
                .then((result) => {
                  var fileStr = fs.readFileSync(result.filePaths[0], {
                    encoding: "binary",
                  });
                  var buf = new Buffer(fileStr, "binary");
                  var str = iconv.decode(buf, "GBK");
                  textarea.value = str;
                })
                .catch((err) => {
                  console.log(err);
                });
            },
          },
        ],
      },
      {
        label: "保存",
        accelerator: `ctrl + s`,
        click: () => {
          dialog
            .showSaveDialog({
              title: "保存文件",
              filters: [
                {
                  name: "txt",
                  extensions: ["txt", "html", "js", "java", "c"],
                },
              ],
            })
            .then((result) => {
              // 获取输入保存
              fs.writeFileSync(result.filePath, textarea.value);
              //保存成功通知
              var option = {
                title: "记事本",
                body: "保存成功",
              };
              new window.Notification(option.title, option);
            })
            .catch((err) => {
              console.log(err);
            });
        },
      },

      {
        label: "退出",
        click: () => {
          window.close();
        },
      },
    ],
  },
  {
    label: "编辑",
    submenu: [
      {
        label: "调节字体",
        submenu: [
          {
            label: "字体大小",
            submenu: [
              {
                label: "增大",
                accelerator: `ctrl + =`,
                click: () => {
                  textarea.style.fontSize =
                    getCss(textarea, "font-size") + 5 + "px";
                },
              },
              {
                label: "减小",
                accelerator: `ctrl + -`,
                click: () => {
                  if (getCss(textarea, "font-size") > 10) {
                    textarea.style.fontSize =
                      getCss(textarea, "font-size") - 5 + "px";
                  } else {
                    textarea.style.fontSize = 10 + "px";
                    alert("已经调到最小了,再小就看不到了");
                  }
                },
              },
            ],
          },
        ],
      },
      {
        label: "更换配色",
        submenu: [
          { label: "默认", click: () => {
            textarea.style.color = '#000000';
            textarea.style.backgroundColor = '#ffffff';
          } },
          { label: "配色方案一" ,
            click:()=>{
              textarea.style.color = '#046582';
              textarea.style.backgroundColor = '#f39189';
            }},
          { label: "配色方案二",
            click:()=>{
              textarea.style.color = '#ffe6ca';
              textarea.style.backgroundColor = '#966c3b';
            } },
        ],
      },
    ],
  },
];
var m = Menu.buildFromTemplate(terMenu);
Menu.setApplicationMenu(m);
